/**
 * Created by lixiaodi on 2019/06/28 0026.
 */
mini.parse();
var grid = mini.get('datagrid1');
//获取当前选项卡
var tab = parent.tabsEl.getActiveTab();
console.log(tab,'当前选项卡');
var noticeType = '';				//文章类型
var articleTitle = '';				//文章标题
var pager = grid.getBottomPager();

//根据条件查询
function onSearch(){
	//得到查询条件数据
	searchData = searchForm.getData();
	//拼接查询sql
	var dataStr = "";       //存放sql
    for(var key in searchData){
    	if(searchData[key]){
    		if(key == 'book_isbn'){
    			dataStr += " and "  + key + " in (" + searchData[key] + ")";
    		}else{
    			dataStr += " and "  + key + " like '%" + searchData[key] + "%'";
    		}
    	}
    }
    var searchSql = dataStr;
	//查询数据
	httpRequest('0',searchSql);
}

//页面初始化
$(function(){
	switch (tab.title){
		case '公告查询':
			noticeType = 1;
			articleTitle = '公告';
			break;
		case '周例会查询':
			noticeType = 3;
			articleTitle = '周例会';
			break;
		case '新闻查询':
			noticeType = 2;
			articleTitle = '新闻';
			break;
	}
	httpRequest();
	//grid.setData(data);
});
//表格的操作按钮绘制
function onActionRenderer(e) {
    var grid = e.sender;
    var record = e.record;
    var uid = record._uid;
    var rowIndex = e.rowIndex;
    var s = ' <a class="mini-button Edit_Button" href="javascript:onSee(\'' + uid + '\')" >查看</a>'
    return s;
}
//查看
function onSee(row_uid){
	var row = grid.getRowByUID(row_uid);
	if(row){
		//先得到缓存并清除缓存
		var localCon = localStorage.getItem("seeArticle");
		if(localCon){
			//清除缓存
			localStorage.removeItem('seeArticle');
		}
		//判断文章正文页面是否打开
		var allOpenTabs = parent.openTabs();
		//如果有新增页面就关闭
		allOpenTabs.forEach(function (item,index){
			var len = item.title.length;
			if(item.title.substr(len-2,2) == '正文'){
				parent.articleTab(item);
			}
		});
		//跳转新增页签
		var noticeData = {
			types: 'see',
			articleData: row
		}
		//将需要编辑的数据带过去
		localStorage.setItem("seeArticle",JSON.stringify(noticeData));
		var jumpUrl = "../pages/booking/noticeManagement/article.html";
		onJump(articleTitle + '正文',jumpUrl);
	}else{
		mini.alert('请选择一条查看');
	}
}
//跳转函数
function onJump(text,jumpUrl){
	//跳转
	var item = {
		id: 'notice',
	    iconCls: "fa fa-desktop",
	    text: text,
	    url: jumpUrl
	};
	//var data = parent.allTabs();
	parent.activeTab(item);//与父页面通信
}
//list
function httpRequest(pageIndex,paramSql){
	var pageObj = {
			noticeType : noticeType,
			pageSize: '500',
			pageNum: pageIndex,
			paramSql: paramSql
	}
	var pageStr = JSON.stringify(pageObj);
	grid.loading('请稍后...');
	$.ajax({
		url: ajaxUrl + '/NoticeNewController/findDeps.do',
		type: 'POST',
		data: pageStr,
		contentType : "application/json",
		success: function(data){
			if(data.status == '706'){
				showTips(tab.title);
				grid.setData([]);
			}else if(data.status == '200'){
				var noticeList = data.data;
				noticeList.forEach((item)=>{
					item.publisher_state = item.publisher_state ? '已发布' : '未发布';
					if(item.creat_date){
						item.creat_date = formatDateSec(item.creat_date);
					}else{
						item.creat_date = '';
					}
					if(item.publisher_date){
						item.publisher_date = formatDateSec(item.publisher_date);
					}else{
						item.publisher_date = '';
					}
				});
				grid.setData(noticeList);
				pager.set({"totalCount":data.countData}); 
			}else{
				mini.alert(data.statusName);
			}
			
		},
		error: function(msg){
			console.log(msg);
			mini.alert('请求失败');
		}
	});
}